<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class ModelWord extends base {

	public function getnew( $num = 14 ) {
		$sql = "select wd.*, memb.name from $this->tword as wd , $this->tmember as memb
				where wd.author=memb.id order by wd.id desc limit $num ";
		$aWord = model::dbuser()->get_all( $sql );
		return $aWord;
	}

	/**
	 * 获得下载的所有的留言
	 *
	 * @return array
	 */
	public function downwords() {
		$sql = "SELECT t1.*, t2.name FROM $this->tword t1, $this->tmember t2 WHERE t1.author=t2.id ORDER BY t1.id";
		$words = model::dbuser()->get_all( $sql );
		return is_array( $words ) ? $words : array();
	}

	/**
	 * 获得部分留言
	 *
	 * @param string $limit 通过分页函数得到的限制查询字符串
	 * @return array
	 */
	public function ls( $limit ) {
		$sql = "SELECT SQL_CALC_FOUND_ROWS t1.*, t2.name, t2.signtext1, t2.signtext2, t2.signtext3, t2.currsign
				FROM $this->tword t1, $this->tmember t2 WHERE t1.author=t2.id ORDER BY t1.id DESC {$limit}";
		$msgs = model::dbuser()->get_all( $sql );
		return is_array( $msgs ) ? $msgs : array();
	}
	
	public function add( $uid, $content ) {
		$sql = "INSERT INTO $this->tword SET author='{$uid}', content='{$content}', time=NOW()";
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}
	
	public function del( $wid ) {
		$sql = "DELETE FROM $this->tword WHERE id='{$wid}' LIMIT 1";
		model::dbuser()->exe ( $sql );
		return model::dbuser()->update_rows();
	}
	
	public function get( $wid ) {
		$sql = "SELECT * FROM $this->tword WHERE id='{$wid}' LIMIT 1";
		return model::dbuser()->get_one( $sql );
	}
	
	public function edit( $wid, $uid, $content ) {
		$sql = "UPDATE $this->tword SET content='{$content}' WHERE author='{$uid}' AND id='{$wid}'";
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}
}